package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 新增时自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //使用若依框架中工具类获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 设置 createTime 字段的值为当前时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        // 设置添加人id
        this.strictInsertFill(metaObject, "creator", Long.class, loginUser.getUserId());
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        //使用若依框架中工具类获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 设置 createTime 字段的值为当前时间
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        // 设置添加人id
        this.strictUpdateFill(metaObject, "updateBy", Long.class, loginUser.getUserId());
    }
}
